package com.ycy.juel;

import org.flowable.common.engine.impl.de.odysseus.el.ExpressionFactoryImpl;
import org.flowable.common.engine.impl.de.odysseus.el.util.SimpleContext;
import org.flowable.common.engine.impl.javax.el.ExpressionFactory;
import org.flowable.common.engine.impl.javax.el.ValueExpression;
import org.junit.Test;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Ex {
  @Test
  public void test1() throws NoSuchMethodException {

    Map<String, Integer> map = new HashMap<>();
    map.put("foo", 1);
    map.put("bar", 333333);

    ExpressionFactory factory = new ExpressionFactoryImpl();
    SimpleContext context = new SimpleContext();

    context.setFunction("shareniu", "max", Math.class.getMethod("max", int.class, int.class));

    for (Map.Entry<String, Integer> stringObjectEntry : map.entrySet()) {
      context.setVariable(stringObjectEntry.getKey(), factory.createValueExpression(stringObjectEntry.getValue(), Integer.class));
    }

    // parse our expression
    ValueExpression e = factory.createValueExpression(context, "${shareniu:max(foo,bar)}", int.class);

// set value for top-level property "bar" to 1
//    factory.createValueExpression(context, "${bar}", int.class).setValue(context, 1);

// get value for our expression
    System.out.println(e.getValue(context)); // --> 1
    System.out.println(e.getValue(context)); // --> 1

  }


  @Test
  public void test11() throws NoSuchMethodException {

    Map<String, List<Long>> map = new HashMap<>();
    List<Long> A = new LinkedList<>();
    map.put("foo", A);
    A.add(1356172011664384002L);
    List<Long> B = new LinkedList<>();
    map.put("bar", B);
    B.add(1356172011664384002L);

    ExpressionFactory factory = new ExpressionFactoryImpl();
    SimpleContext context = new SimpleContext();

    context.setFunction("RDFAbpJuelService", "retain", RDFAbpJuelForSkipTaskService.class.getMethod("retain", List.class, List.class));

    for (Map.Entry<String, List<Long>> stringObjectEntry : map.entrySet()) {
      context.setVariable(stringObjectEntry.getKey(), factory.createValueExpression(stringObjectEntry.getValue(), List.class));
    }

    // parse our expression
    ValueExpression e = factory.createValueExpression(context, "${RDFAbpJuelService:retain(foo,bar)}", boolean.class);

// get value for our expression
    System.out.println(e.getValue(context)); // --> true

  }

  @Test
  public void test2() {
//    String ex = String.format("${ COND_%1$s_approve == null || nrOfCompletedInstances == null" +
//            " || COND_%1$s_approve/COND_%1$s_nrOfCompletedInstances>=0.5}", "a");

    String ex = String.format("${ COND_%1$s_approve/COND_%1$s_nrOfCompletedInstances>=0.5 }", "a");

    boolean b = checkExpression(ex, new HashMap<>());
    System.out.println(b);
  }

  public static boolean checkExpression(String expression, Map<String, Object> map) {
    try {
      ExpressionFactory factory = new ExpressionFactoryImpl();
      SimpleContext context = new SimpleContext();
      for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
        context.setVariable(stringObjectEntry.getKey(), factory.createValueExpression(stringObjectEntry.getValue(), Integer.class));
      }
      ValueExpression valueExpression = factory.createValueExpression(context, expression, Boolean.class);
      return (boolean) valueExpression.getValue(context);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

}
